/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wanhai.itm.util;

import com.wanhai.ejb.dbhandler.DBHandlerLocal;
import com.wanhai.itm.liveListModel.LiveListModel;
import java.util.ArrayList;
import java.util.List;

public class VoDataService<T>{
    private DBHandlerLocal local;
    String _DataSource = "java:/OraWhl2DS";
    private String orderBy = "";
    
    public VoDataService(){
            initConn();
    }
    
    public List<T> find(T cond) throws Exception{
        ArrayList<T> rtnValues = new ArrayList<T>();
        rtnValues = getDBHandler().query(cond);
        
        return rtnValues;
    }
    public int insert(T target) throws Exception{
        return getDBHandler().insert(target);
    }
    public int update(T target) throws Exception{
        return getDBHandler().update(target);
    }
    public void delete(T target) throws Exception{
        getDBHandler().delete(target);
    }
    
    private void setDBHandler(DBHandlerLocal local) {
        this.local = local;
    }
    
    public DBHandlerLocal getDBHandler() {
        return this.local;
    }
    
    public String getSQL(){
        return this.local.getSQL();
    }
    
    private void initConn(){
        DBHandlerLocal local = new DBHandlerLocal();
        local.setConnection(_DataSource);
        local.setQuerySize(-1);
        this.setDBHandler(local);
    }
    
    public LiveListModel<T> queryLive(T cond) throws Exception{
        LiveListModel liveModel = new LiveListModel<T>(local,cond);
        liveModel.setOrderBy(this.orderBy);
        
        return liveModel;
    }
    
    public void setOrderBy(String orderBy){
        this.orderBy = orderBy;
    }
}
